package io.seata.samples.nacos.call.controller;

import io.seata.samples.nacos.call.service.BusinessService;
import io.seata.samples.nacos.common.dto.BusinessDTO;
import io.seata.samples.nacos.common.response.ObjectResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Dubbo业务执行入口
 *
 * @author Administrator
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/business")
public class BusinessController {
    private final BusinessService businessService;

    /**
     * 模拟用户购买商品下单业务逻辑流程，不进行回滚
     * 请求参数示例：{"userId": 1, "commodityCode":"C201901140001", "count": 10, "amount": 10, "throwEx": false}
     */
    @PostMapping("/testCommit")
    ObjectResponse handleBusiness(@RequestBody BusinessDTO businessDTO) {
        log.info("请求参数：{}", businessDTO.toString());
        businessDTO.setThrowEx(false);
        return businessService.handleBusiness(businessDTO);
    }

    /**
     * 模拟用户购买商品下单业务逻辑流程，进行回滚
     * 请求参数示例：{"userId": 1, "commodityCode":"C201901140001", "count": 10, "amount": 10, "throwEx": true}
     */
    @PostMapping("/testRollback")
    ObjectResponse handleBusiness2(@RequestBody BusinessDTO businessDTO) {
        log.info("请求参数：{}", businessDTO.toString());
        businessDTO.setThrowEx(true);
        return businessService.handleBusiness(businessDTO);
    }
}
